package org.epam.szhuk.greenway.asynctask;

import java.util.ArrayList;
import java.util.List;

import org.epam.szhuk.greenway.R;
import org.epam.szhuk.greenway.activity.RecordedPathActivity;
import org.epam.szhuk.greenway.activity.SelectLocationFilesActivity;
import org.epam.szhuk.greenway.model.LocationFileSelection;
import org.epam.szhuk.greenway.model.StoredGeoPoint;
import org.epam.szhuk.greenway.util.GeoDataProcessor;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
/**
 * 
 * @author Sergey
 *
 * solution from: http://stackoverflow.com/questions/7713222/android-progress-dialog
 * 
 */
public class LoadLocationDataTask extends AsyncTask<Integer, Integer, Boolean>{
	  private final static String TAG="LoadLocationDataTask";
	  private ProgressDialog progressDialog;
      private List<List<StoredGeoPoint>> multipleStoredGeoPointLists;
      //private List<LocationFileSelection> listFiles; 
      private Context context;
      private List<String> listSelectedFiles;
      public LoadLocationDataTask(Context context, List<String> listSelectedFiles){
    	  this.context=context;
    	  this.listSelectedFiles=listSelectedFiles;
      }
      
      @Override
      protected void onPreExecute() {
          /*
           * This is executed on UI thread before doInBackground(). It is
           * the perfect place to show the progress dialog.
           */
          progressDialog = ProgressDialog.show(context, "", "Loading...");
      }

      @Override
      protected Boolean doInBackground(Integer... params) {
          if (params == null) {
              return false;
          }
          try {
              /*
               * This is run on a background thread, so we can sleep here
               * or do whatever we want without blocking UI thread.
               */
      		
      		if(listSelectedFiles.size()>0){	
      			multipleStoredGeoPointLists=new ArrayList<List<StoredGeoPoint>>();
      			for(int j=0; j<listSelectedFiles.size(); j++){
      				List<StoredGeoPoint> storedGeoPointsList=(List<StoredGeoPoint>)GeoDataProcessor.getGeoPointsForFile(context,listSelectedFiles.get(j));
      				if(storedGeoPointsList!=null){
      					multipleStoredGeoPointLists.add(storedGeoPointsList);
      				}
      			}		
      		}
          }
          catch (Exception e) {
              Log.e(TAG, e.getMessage());
              //The task failed
              return false;
          }
           //* The task succeeded
          return true;
      }

      @Override
      protected void onPostExecute(Boolean result) {
          /*
           *  we are working on UI thread.
           */
    	  progressDialog.dismiss();
	      if((multipleStoredGeoPointLists!=null)&&(multipleStoredGeoPointLists.size()>0)){
	    	  	
	    		Intent recordedPathActivity = new Intent(context, RecordedPathActivity.class);
	      		recordedPathActivity.putExtra("multipleStoredGeoPointLists", (ArrayList)multipleStoredGeoPointLists);
	      		context.startActivity(recordedPathActivity);
	  	  }
  		  else{
  		    	myToast(context.getResources().getString(R.string.cant_get_info_for_selected_probably_location_na));
  		  }
      }
      
  	private void myToast(String text) {
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}
  }

